package com.hd.service.doc.article.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.hd.entity.Attach;
import com.hd.service.AttachService;
import com.hd.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hd.dao.BaseDao;
import com.hd.entity.doc.article.Article;
import com.hd.service.doc.article.ArticleService;
import com.hd.util.result.PageInfo;
/**
* 文章
*/
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private BaseDao<Article,String>articleDao;
    @Autowired
    private AttachService attachService;
    @Override
    public void save(Article article) {
    	article.setId(UUID.randomUUID().toString());
       articleDao.save(article);
    }
    
    @Override
    public void delete(String id){
      articleDao.delete(Article.class,id);
    }
       

    @Override
    public void update(Article article) {
        Article vo=articleDao.get(Article.class,article.getId());
        articleDao.update(article);
    }
    @Override
    public Article get(String id){
        Article article=articleDao.get(Article.class, id);
        if(article!=null){
            List<Attach>attachs=new ArrayList<Attach>();
            if(StringUtils.isNotNull(article.getAttachIds())){
                String articleIds[]=article.getAttachIds().split(",");
                for(String attachId:articleIds){
                    Attach attach=attachService.get(attachId);
                    if(attach!=null){
                        attachs.add(attach);
                    }
                }
            }
            article.setAttachs(attachs);
        }
    	return article;
    }
        

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        String hql="from Article where 1=1 ";
        Map<String,Object>condition=pageInfo.getCondition();
        List<Object> params=new ArrayList<Object>();
        if(StringUtils.isNotNull(condition.get("type"))){
            hql+=" and type=?";
            params.add(condition.get("type"));
        }
        if(StringUtils.isNotNull(condition.get("title"))){
            hql+=" and title like ?";
            params.add("%"+condition.get("title")+"%");
        }
        if(!pageInfo.getSort().equals("")) {
            hql+=" order by "+pageInfo.getSort()+" "+pageInfo.getOrder();
        }
        String countSql="select count(*) "+hql;
        List<Article> articles=articleDao.findPage(hql, pageInfo.getNowpage(), pageInfo.getPagesize(),params);
        if(articles.size()>0){
            for(Article article:articles){
                List<Attach>attachs=new ArrayList<Attach>();
                if(StringUtils.isNotNull(article.getAttachIds())){
                    String articleIds[]=article.getAttachIds().split(",");
                    for(String attachId:articleIds){
                        Attach attach=attachService.get(attachId);
                        if(attach!=null){
                            attachs.add(attach);
                        }
                    }
                }
                article.setAttachs(attachs);
            }
        }
        pageInfo.setRows(articles);
        Long total=articleDao.getCountByList(countSql,params);
        pageInfo.setTotal(Integer.parseInt(total.toString()));
    }



}
